package com.sunchangpeng.wechat.mp.media;

import com.alibaba.fastjson.JSON;
import com.sunchangpeng.wechat.core.WechatClient;
import com.sunchangpeng.wechat.mp.EndpointProvider;
import com.sunchangpeng.wechat.mp.MpAppSetting;
import com.sunchangpeng.wechat.mp.MpWechatClientFactory;
import com.sunchangpeng.wechat.mp.media.bean.Media;
import com.sunchangpeng.wechat.mp.media.bean.MediaType;

import java.io.File;

import static com.sunchangpeng.wechat.core.utils.StringPool.MEDIA_NAME;

public class Medias {
    private WechatClient wechatClient;

    public void setWechatClient(WechatClient wechatClient) {
        this.wechatClient = wechatClient;
    }

    public static Medias with(MpAppSetting appSetting) {
        Medias medias = new Medias();
        medias.setWechatClient(MpWechatClientFactory.getInstance().with(appSetting));
        return medias;
    }

    /**
     * 新增图片（image）临时素材
     * 2M，支持PNG\JPEG\JPG\GIF格式
     */
    public Media uploadImage(String fileName, byte[] content) {
        return upload(MediaType.image, fileName, content);
    }

    /**
     * 获取图片（image）临时素材
     */
    public File downloadImage(String mediaId) {
        return download(mediaId);
    }


    /**
     * 新增语音（voice）临时素材
     * 2M，播放长度不超过60s，支持AMR\MP3格式
     */
    public Media uploadVoice(String fileName, byte[] content) {
        return upload(MediaType.voice, fileName, content);
    }

    /**
     * 获取语音（voice）临时素材
     */
    public File downloadVoice(String mediaId) {
        return download(mediaId);
    }


    /**
     * 新增视频（video）临时素材
     * 10MB，支持MP4格式
     */
    public Media uploadVideo(String fileName, byte[] content) {
        return upload(MediaType.video, fileName, content);
    }

    /**
     * 获取视频（video）临时素材video_url
     */
    public String getVideoUrl(String mediaId) {
        String response = wechatClient.getTextWithToken(String.format(EndpointProvider.get("url.media.get"), mediaId));
        return JSON.parseObject(response).getString("video_url");
    }

    /**
     * 新增缩略图（thumb）临时素材
     * 64KB，支持JPG格式
     */
    public Media uploadThumb(String fileName, byte[] content) {
        return upload(MediaType.thumb, fileName, content);
    }

    /**
     * 获取缩略图（thumb）临时素材
     */
    public File downloadThumb(String mediaId) {
        return download(mediaId);
    }

    private Media upload(MediaType type, String fileName, byte[] content) {
        String url = EndpointProvider.get("url.media.upload");
        String response = wechatClient.multiFormWithToken(String.format(url, type.name()), MEDIA_NAME, fileName, content);
        return JSON.parseObject(response, Media.class);
    }

    /**
     * 获取临时素材，不支持视频文件的下载
     * 请注意，视频文件不支持https下载，调用该接口需http协议。
     */
    private File download(String mediaId) {
        return wechatClient.getTempFileWithToken(String.format(EndpointProvider.get("url.media.get"), mediaId));
    }

    /**
     * 高清语音素材获取
     *
     * @param mediaId 媒体文件ID，即uploadVoice接口返回的serverID
     */
    public File downloadHDVoice(String mediaId) {
        return wechatClient.getTempFileWithToken(String.format(EndpointProvider.get("url.media.get.jssdk"), mediaId));
    }
}
